package com.yurun.loginmodule.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.OrganizationInfo;
import com.yurun.organizationmodule.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lihao
 * @mail lihaohaocn@gmail.com
 * @Date 2021/7/27 7:46 下午
 */

@Api(tags="机构管理")
@RestController
@RequestMapping("/organizationManage")
public class OrganizationManageController {

    @Autowired
    private OrganizationService organizationService;


    /**
     *
     * 查看所有机构信息
     */
    @ApiOperation("查看所有机构信息")
    @RequestMapping("/getOrganizationList")
    @RequiresPermissions("/getOrganizationList")
    public CommonResult getAllOrganizationList(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrganizationInfo> list = organizationService.getOrganizationList(keyword);
        if (list.size() == 0) {
            return CommonResult.failed("对不起，暂时查询不到机构信息！");
        }
        return CommonResult.success(list);
    }

    /**
     *
     * 查找所有机构生成Option
     */
    @ApiOperation(value = "查找所有机构生成Option")
    @RequestMapping("/organizationOption")
    public CommonResult getOrgOption(){
        return CommonResult.success(organizationService.getOrgOption());
    }

    /**
     *
     * 增加机构
     */
    @ApiOperation("增加机构")
    @RequiresPermissions("/addOrganization")
    @RequestMapping(value = "/addOrganization", method = RequestMethod.POST)
    public CommonResult addOrg(OrganizationInfo organizationInfo) {
        if (organizationService.addOrg(organizationInfo)) {
            return CommonResult.success(true);
        }
        return CommonResult.failed();
    }

    /**
     *
     * 通过机构名查看信息
     */
    @ApiOperation("通过机构名查看信息")
    @RequiresPermissions("/oneOrgInfoByName")
    @RequestMapping("/oneOrgInfoByName")
    public CommonResult getOneOrgInfoByName(String organizationName, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<OrganizationInfo> list = organizationService.getOneOrgInfoByName(organizationName);
        if (list.size() == 0) {
            return CommonResult.failed("对不起，暂无此机构！");
        }
        return CommonResult.success(list);
    }

    /**
     *
     * 通过机构id查看信息
     */
    @ApiOperation("通过机构id查看信息")
    @RequiresPermissions("/oneOrgInfoById")
    @RequestMapping("/oneOrgInfoById")
    public CommonResult getOneOrgInfoByName(BigInteger organizationId){
        return CommonResult.success(organizationService.getOneOrgInfoById(organizationId));
    }

    /**
     * 通过机构id修改信息
     */
    @ApiOperation("通过机构id修改信息")
    @RequiresPermissions("/editOrgById")
    @RequestMapping("/editOrgById")
    public CommonResult editOrgById(OrganizationInfo organizationInfo){
        if (organizationService.editOrgById(organizationInfo)) {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.failed();
    }

    /**
     * 删除机构
     */
    @ApiOperation("删除机构")
    @RequiresPermissions("/delOrgById")
    @RequestMapping("/delOrgById")
    public CommonResult delOrgById(BigInteger organizationId){
        if (organizationService.delOrgById(organizationId)) {
            return CommonResult.success("成功删除！");
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取机构管理员账号")
    @RequestMapping("/getAdminEmployeeId")
    public CommonResult getAdminEmployeeId(BigInteger organizationId) {
        return organizationService.getAdminEmployeeId(organizationId);
    }

}
